package appapi.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @version: java version 1.8
 * @Author: Ygp Work
 * @description: 设备调试redis工具类
 * @date: 2023-07-27 17:21
 */
public class EquipTestUtils {
    /**
     * REDIS别头
     */
    private static final String REDIS_ORDER = "keda:import:equipType_";


    /**
     * 有效期
     */
    public static final Long ORDER_EXPIRE = 20L;
    /**
     * 别头
     */
    private static final String ORDER = "";
    /**
     * 随即编码
     */
    private static final int[] r = new int[]{7, 9, 6, 2, 8, 1, 3, 0, 5, 4};
    /**
     * 用户id和随机数总长度
     */
    private static final int maxLength = 6;

    /**
     * 根据id进行加密+加随机数组成固定长度编码
     */
    private static String toCode(Long userId) {
        String idStr = userId.toString();
        StringBuilder idsbs = new StringBuilder();
        for (int i = idStr.length() - 1; i >= 0; i--) {
            idsbs.append(r[idStr.charAt(i) - '0']);
        }
        return idsbs.append(getRandom(maxLength - idStr.length())).toString();
    }

    /**
     * 生成时间戳
     */
    private static String getDateTime() {
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }


    /**
     * 生成固定长度随机码
     *
     * @param n 长度
     */
    private static long getRandom(long n) {
        long min = 1, max = 9;
        for (int i = 1; i < n; i++) {
            min *= 10;
            max *= 10;
        }
        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
        return rangeLong;
    }

    /**
     * 生成不带类别标头的编码
     *
     * @param userId
     */
    private static synchronized String getCode(Long userId) {
        userId = userId == null ? 10000 : userId;
        return toCode(userId);
    }

    /**
     * 生成订单号(调用方法)
     *
     * @param userId 网站中该用户唯一ID 防止重复
     */
    public static String getProjectNo(Long userId) {
        return getDateTime().substring(2, 10) + getCode(userId);
    }

    /**
     * 生成Redis项目ID
     *
     * @param projectNo 项目ID
     */
    public static String getCashRrdisRightingProjectNoKey(String projectNo) {
        return REDIS_ORDER + projectNo;
    }

    /**
     * 生成Redis项目记录数ID
     *
     * @param projectNo 项目ID
     */
    public static String getCRedisCountKey(String projectNo) {
        return REDIS_ORDER + "count" + projectNo;
    }

    /**
     * 生成Redis项目统计信息ID
     *
     * @param projectNo 项目ID
     */
    public static String getRedisStaticKey(String projectNo) {
        return REDIS_ORDER + "static" + projectNo;
    }

    /**
     * 生成Redis项目ID
     *
     * @param projectNo 项目ID
     */
    public static String getRedisProjectNoKey(String projectNo) {
        return REDIS_ORDER + projectNo;
    }

    /**
     * 生成Redis项目记录数ID
     *
     * @param projectNo 项目ID
     */
    public static String getRedisRecordCountKey(String projectNo) {
        return REDIS_ORDER + "count" + projectNo;
    }

}
